package indi.kider.server.woniubus.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.woniubus.NoticeVo;
import indi.kider.server.woniubus.service.NoticeSerice;
import indi.kider.server.woniubus.service.related.AuthcService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import java.util.List;
import java.util.Map;

/**
 * 通知接口服务
 */
@RestController
@RequestMapping(value = "notice")
@Log
public class NoticeController {

    @Autowired
    private NoticeSerice noticeSerice;

    @Autowired
    private AuthcService authcService;

    /**
     * 上位企业管理员查询/合作企业员
     * 根据字段查询获取通知列表（分页）
     */
    @PostMapping("/getNoticeList")
    public CommonResponse<List<Map>> getNoticeList(@RequestBody PageRequest<NoticeVo> request) {
        Employee employee = authcService.getCurrentUser().getData();

        if(employee.getRoles().equals(EmployeeRoleCons.FIVE)) {
            return CommonResponse.buildGetSuccessResponse(noticeSerice.getNoticeListForPart(request,employee));
        }
        return CommonResponse.buildGetSuccessResponse(noticeSerice.getNoticeListForSystem(request,employee));
    }

    /**
     * 上位企业管理员/合作企业员
     * 发送通知
     */
    @PutMapping("/sendNotice")
    public CommonResponse sendNotice(@RequestBody NoticeVo noticeVo) {
        Employee employee = authcService.getCurrentUser().getData();
        try {
            if(employee.getRoles().equals(EmployeeRoleCons.FIVE)) {
                return  noticeSerice.sendNoticeByPart(noticeVo, employee);
            }
            return  noticeSerice.sendNoticeBySystem(noticeVo, employee);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 上位企业管理员/合作企业员
     * 发送通知
     */
    @PutMapping("/sendNoticeByHouseList")
    public CommonResponse sendNoticeByHouseList(@RequestBody NoticeVo noticeVo) {
        Employee employee = authcService.getCurrentUser().getData();
        try {
            if(employee.getRoles().equals(EmployeeRoleCons.FIVE)) {
                return  noticeSerice.sendNoticeByPartByHouseList(noticeVo, employee);
            }
            return  noticeSerice.sendNoticeBySystemByHouseList(noticeVo, employee);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
